/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.view;

import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.StringItem;

import com.aroundj2me.controller.ScreenController;
import com.aroundj2me.utility.ImageLoader;

/**
 * For displaying running progress on the screen
 *
 */
public class ProgressForm extends Form implements Runnable, IProgressWindow {
	private Gauge gauge;
	private StringItem message;
	private int SLEEP_INTERVAL = 1; // in ms
	private boolean progressRunning = false;
	
	/**
	 * Default constructor
	 *
	 */
	public ProgressForm(){
		super("loading...");
		initComponents();
	}
	
	/**
	 * Initializes the visual components
	 *
	 */
	private void initComponents(){
		ImageItem img = new ImageItem("", ImageLoader.getImage(ImageLoader.GLOBSEARCH), ImageItem.LAYOUT_CENTER, null);
		message = new StringItem("loading...", null);
		message.setLayout(StringItem.LAYOUT_NEWLINE_AFTER);
		message.setLayout(StringItem.LAYOUT_CENTER);
		gauge = new Gauge(null, false, 50, 0);
		gauge.setLayout(Gauge.LAYOUT_NEWLINE_BEFORE);
		gauge.setLayout(Gauge.LAYOUT_CENTER);
		this.append(img);
		this.append(message);
		this.append(gauge);
	}
	
	/**
	 * Starts the progressbar with the default
	 * message
	 */
	public void startProgress(){
		gauge.setValue(0);
		progressRunning = true;
		Thread t = new Thread(this);
		t.start();
		ScreenController.getInstance().display(this);
	}
	
	/**
	 * Starts the progressbar and visualizes the
	 * given message
	 * @param progressMessage = message to display while loading
	 */
	public void startProgress(String progressMessage){
		gauge.setValue(0);
		message.setLabel(progressMessage);
		startProgress();
	}
	
	/**
	 * Stops the progressbar and the thread which is
	 * incrementing it
	 *
	 */
	public void stopProgress() {
		ScreenController.getInstance().last();
		progressRunning = false;
	}
	
	/**
	 * Run method of the thread which increments the progressbar
	 */
	public void run() {
        int val = gauge.getValue();
        try {
            while(progressRunning){
            	Thread.sleep(SLEEP_INTERVAL);
	            gauge.setValue(val++);
                if(val >= gauge.getMaxValue()){
                	gauge.setValue(gauge.getMaxValue());
                	Thread.sleep(SLEEP_INTERVAL*2);
                	val = 0;
                }
            }
        }
        catch( InterruptedException e ){
        	e.printStackTrace();
        }
	}
}
